package com.rjyb.student_management_platform.service.Impl;

import com.rjyb.student_management_platform.dao.TeacherDao;
import com.rjyb.student_management_platform.pojo.*;
import com.rjyb.student_management_platform.pojo.Class;
import com.rjyb.student_management_platform.service.TeacherService;
import com.rjyb.student_management_platform.unit.common.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TeacherServiceImpl implements TeacherService {
    @Autowired
    TeacherDao teacherDao;

    DateTime dateTime = new DateTime();

    //查询班级
    @Override
    public List<Map> selclass(int id) {
        return teacherDao.selclass(id);
    }

    //查询学生
    @Override
    public List<Student> selestudent(int cid,int tid) {
        return teacherDao.selestudent(cid,tid);
    }
    //老师注册
    @Override
    public Teacher ifTeacher(String name, String jobNumber) {
        return teacherDao.ifTeacher(name,jobNumber);
    }


    //老师查本人发布过的作业
    @Override
    public List<Map> selejob(Teacher teacher, int cid) {
        long tid = teacher.getTid();
        long type = teacher.getType();
        if(type==1){
            List<Map> jobtables = teacherDao.selejob(tid,cid);
            return jobtables;
        }else {
            List<Map> jobtables = teacherDao.selejob1(cid);
            return jobtables;
        }


    }

    @Override
    public int seleclassjob(int cid) {

        return teacherDao.selecpnum(cid);
    }

    @Override
    public int seleclassjob1(int jid) {
        return teacherDao.selecpnum1(jid);
    }

    @Override
    public int seleclassjob2(int jid) {
        return teacherDao.selecpnum2(jid);
    }

    @Override
    public int seleclassjob3(int jid) {
        return teacherDao.selecpnum3(jid);
    }

    @Override
    public List<AnswerSheet> selectclass(int jid) {
        return teacherDao.selectclass(jid);
    }

    @Override
    public List<Student> selectclass1(List list, int cid) {
        if (list.size()==0){
            return teacherDao.selectclass2(cid);
        }
        return teacherDao.selectclass1(list,cid);
    }

    //按照老师id查询班级
    @Override
    public List<Class> selectClass(HttpSession session, Model model) {
       Teacher teacher = (Teacher) session.getAttribute("teacher");
        return teacherDao.selectClass(teacher.getTid());
    }

    //按照班级查询学生
    @Override
    public String selectStu(Class c, Model model, HttpSession session) {
       List<Class> classList = selectClass(session,model);
       List<Map> studentList = teacherDao.selectStu(c);
       model.addAttribute("stu",studentList);
       model.addAttribute("classList",classList);
       model.addAttribute("c",c);
        return "teacher/addAttendance";
    }
    //添加考勤
    @Override
    public String attendanceAdd(Long[] sid, Long cid, Long classSection, Long[] atRate, HttpSession session, Model model) {
        List<Map<String,Long>> mapList = new ArrayList<>();
        for (int i = 0; i < sid.length; i++) {
            Map<String,Long> att = new HashMap<>();
            att.put("sid",sid[i]);
            att.put("atRate",atRate[i]);
            mapList.add(att);
        }
        String adDate =dateTime.nowTime().substring(0,11);
        Teacher teacher = (Teacher) session.getAttribute("teacher");
        //查询这个老师是否添加过今天的这节课
       List<Attendance> att = teacherDao.selectAtt(classSection,adDate,cid);
        if(att.size()==0){
          // List<Class> classList = teacherDao.selectClass(teacher.getTid());
          //  model.addAttribute("classList",classList);
            teacherDao.attendanceAdd(mapList,cid,classSection,adDate,teacher.getTid());
           // model.addAttribute("no","添加考勤成功");
            return "redirect:Attendance_inquiry?cid="+cid+"&time="+adDate+"&class_section="+classSection;
        }else {
            List<Class> classList = teacherDao.selectClass(teacher.getTid());
            model.addAttribute("classList",classList);
            model.addAttribute("no","请勿提交重复的考勤");
        }
        return "teacher/addAttendance";

    }

    //添加老师
    @Override
    public boolean addTeacher(Teacher teacher) {
        return teacherDao.addTeacher(teacher);
    }


    //老师id查询班级
    @Override
    public List<Class> toCheckattendance(HttpSession session) {
        Teacher teacher = (Teacher) session.getAttribute("teacher");
        long tid = teacher.getTid();
        return teacherDao.toCheckattendance(tid);
    }

    //查询考勤
    @Override
    public List<Map> selectattendance(long cid, String time, long class_section,Map map,HttpSession session) {
        Teacher teacher = (Teacher) session.getAttribute("teacher");
        long type = teacher.getType();
        long tid = teacher.getTid();
        if (type==1){
            if(cid==-1 || time==null || class_section==-1){
                map.put("erorr","补全信息");

            }else {
                return teacherDao.selectattendance1(cid,time,class_section,tid);
            }
        }else {
            if(cid==-1 || time==null || class_section==-1){
                map.put("erorr","补全信息");

            }else {
                return teacherDao.selectattendance(cid,time,class_section);
            }
        }

        return null;
    }

    //根据条件查询考勤老师name
    @Override
    public List<Map> tname(long cid, String time, long class_section, Map map, HttpSession session) {
        return teacherDao.tname(cid,time,class_section);
    }

    //修改考勤
    @Override
    public String attendanceModify(Long class_section, Long sid, String atDate, Long cid, Long atRate) {
        teacherDao.attendanceModify(class_section,sid,atDate,cid,atRate);
        return "修改成功";
    }
    //主页本班作业
    @Override
    public Integer selectAttendance(long tid, String s) {
        return teacherDao.selectAttendance(tid,s);
    }

    //主页本周查询考勤
    @Override
    public List<Map> selectCompletionRate(long tid) {
        return teacherDao.selectCompletionRate(tid);
    }

    //主页查看本天课程考勤
    @Override
    public Integer selectAttendanceForTheDay(long tid, String at, long cs) {
        return teacherDao.selectAttendanceForTheDay(tid,at,cs);
    }



    //查询已完成作业类型
    @Override
    public List<Map> selectjob1(int jid, int cid) {
        return teacherDao.selectjob1(jid,cid);
    }

    //查询已完成作业学生
    @Override
    public List<Map> selectjob2(int jid, int cid) {
        return teacherDao.selectjob2(jid,cid);
    }

    //班导查看本周考勤
    @Override
    public Integer selectClassGuideAttendance(long tid, String s) {
        return teacherDao.selectClassGuideAttendance(tid,s);
    }

    //班导查询作业完成率 查看班级
    @Override
    public List<Map> selectClassGuideCompletionRate(long tid) {
        return teacherDao.selectClassGuideCompletionRate(tid);
    }

    //班导查看本天课程考勤
    @Override
    public Integer selectClassGuideAttendanceForTheDay(long tid, String at, long cs) {
        return teacherDao.selectClassGuideAttendanceForTheDay(tid,at,cs);
    }

    //班导查询本班本周已到人数
    @Override
    public Integer selectAttendanceHasArrived(long tid, String adDate,int class_section) {
        return teacherDao.selectAttendanceHasArrived(tid,adDate,class_section);
    }

    //班导查询本班人数
    @Override
    public Integer selectPeople(long tid, String atDate) {
        return teacherDao.selectPeople(tid,atDate);
    }

}


